﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Graph;
using SQLite;
using SearchResult = TabbedTemplate.Models.SearchResult;

namespace TabbedTemplate.Services
{
    public class StoreResult:ISearchResultService {
        private IPreferenceStorage _preferenceStorage;
        public StoreResult(IPreferenceStorage preferenceStorage)
        {
            _preferenceStorage = preferenceStorage;
        }
        /// <summary>
        /// 搜索内容数据库名。
        /// </summary>
        public const string DbName = "queryStoresdb.sqlite3";

        // ******** 私有变量
        /// <summary>
        /// 搜索内容数据库路径。
        /// </summary>
        /// <returns></returns>
        public static readonly string QueryDbPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder
                .LocalApplicationData), DbName);

        /// <summary>
        /// 数据库连接影子变量。
        /// </summary>
        private SQLiteAsyncConnection _connection;

        /// <summary>
        /// 数据库连接。
        /// </summary>
        public SQLiteAsyncConnection Connection
        {
            get => _connection ??= new SQLiteAsyncConnection(QueryDbPath);
            set => _connection = value;
        }
        public bool Initialized() =>
            _preferenceStorage.Get(QueryServiceConstants.VersionKey, -1) ==
                QueryServiceConstants.Version;
        

        public async Task InitializeAsync() {
            await Connection.CreateTableAsync<SearchResult>();
            _preferenceStorage.Set(Services.QueryServiceConstants.VersionKey,
                Services.QueryServiceConstants.Version);
        }

        public async Task InsertSearchItem(string uid, int id,long TimeStamp) {

            await ConnectionInsertAsync(new SearchResult {
                SearchItemCache_Id= id,
                Store_Uid = uid,
                InsertTime = TimeStamp
        });
        }
        public async Task ConnectionUpdateAsync(SearchResult searchResult)
        {
            await Connection.UpdateAsync(searchResult);
        }

        public async Task ConnectionInsertAsync(SearchResult searchResult)
        {
            await Connection.InsertAsync(searchResult);
        }
        public async Task<IList<SearchResult>> GetStoresById(int id) =>
            await Connection.Table<SearchResult>().Where(p => p.SearchItemCache_Id == id)
                .ToListAsync();
        

        public async Task CloseAsync() {
            throw new NotImplementedException();
        }

        public async Task DeleteCacheById(int id) {
            var res = await Connection.Table<SearchResult>().ToListAsync();
            for (int i = 0; i < res.Count; i++) {
                if (res[i].SearchItemCache_Id == id) {
                    await Connection.DeleteAsync(res[i]);
                }
            }
        }

        public static class QueryServiceConstants
        {
            /// <summary>
            /// 版本。
            /// </summary>
            public const string VersionKey =
                nameof(IQueryService) + "." + nameof(Version);

            /// <summary>
            /// 版本。
            /// </summary>
            public const int Version = 1;
        }
    }

}
